<?php
class Quanlymodel extends Model
{
	var $m_upload_path = 'uploads/images/';
	
	var $m_error = array();
	function _isImageColumn($columns, $imageColumns)
	{
		if(!isset($imageColumns))
			return FALSE;
		foreach($imageColumns as $r)
		{
			if($columns == $r)
				return TRUE;
		}
		return FALSE;
	}
	function Quanlymodel()
	{
		parent::Model();
		$this->load->database();
		$this->load->helper('form');
		$this->load->helper('url');
		$this->load->library('upload');
		$this->load->library('image_lib');
		$this->load->helper('file');
	}
	function xoa($table, $id, $keyField=NULL)
	{
		if($table=='tbluser'){
			
			$this->db->where('id',$id);
			$query = $this->db->get('tbluser');
			//echo $id;
			$taikhoan = $query->row()->taikhoan;
			$this->db->where('taikhoan', $taikhoan);
			$this->db->delete('tbluser'); 
		}



		// neu bang co nhieu khoa chinh, tham so $id va $keyField se la mot mang
		if(is_array($id))
		{
			if(!isset($keyField))
			{
				$query = $this->db->query('show columns from ' . $table);
				foreach($query->result() as $row)
				{		
					if($row->Key == 'PRI')
						$keyField[] = $row->Field;
				}
			}
			
			$n = count($keyField);
			for($i=0; $i<$n; $i++)
				$this->db->where($keyField[$i], $id[$i]);
			$this->db->delete($table);
		}
		// bang chi co mot khoa chinh, $id chi la mot bien
		else
		{
			///neu khoa chinh cua bang chua duoc cung cap, tim khoa chinh cua bang
			if(!isset($keyField))
			{
				$query = $this->db->query('show columns from ' . $table);
				foreach($query->result() as $row)
				{
					if($row->Key == 'PRI')
					{
						$keyField = $row->Field;
						break;
					}
				}
			}
			///ket thuc tim khoa chinh cua bang
			
			$this->db->where($keyField, $id);
			$this->db->delete($table);
		}
		
		
	}
	function them($table, $imageColumns=NULL, $thumbnailColumns=NULL, $thumbnailWidth=0, $thumbnailHeight=0)
	{
		$data = array();
		$query = $this->db->query('show columns from ' . $table);
		$columns = $query->result();
		$upload_datas = array();
		foreach($columns as $column)
		{
			if($column->Type == 'datetime' || $column->Type == 'date')
			{
				$data[$column->Field] = date('Y-m-d H:i:s', get_form_date_value($column->Field));
			}
			else if($this->_isImageColumn($column->Field, $imageColumns))
			{
				/*$config['upload_path'] = './' . $this->m_upload_path;
				$config['allowed_types'] = 'gif|jpg|png|bmp|swf|zip|rar|doc|pdf';
				$config['max_size'] = '1048576';
				$config['max_width'] = '32768';
				$config['max_height'] = '32768';
				$this->load->library('upload', $config);
				$this->upload->initialize($config);
				if(!$this->upload->do_upload($column->Field))
				{
//					$this->m_error[] = $this->upload->display_errors();
//					return FALSE;
					$data[$column->Field] = '';
				}
				else
				{
					$upload_data = $this->upload->data();
					$data[$column->Field] = $this->m_upload_path . $upload_data['file_name'];
					$upload_datas[$column->Field] = $upload_data;
				}*/
				$hinhanh = $this->do_upload_file($column->Field);
				
/*				$upload_data = $this->upload->data();
				if(!empty($hinhanh)){
					$data[$column] = $this->m_upload_path . $hinhanh['file_name'];
				}else{
					$data[$column]='';
				}
				$upload_datas[$column] = $hinhanh;
*/				
				$upload_data = $this->upload->data();
				if(!empty($hinhanh))
					$data[$column->Field] = $this->m_upload_path . $hinhanh['file_name'];
				else
					$data[$column->Field] = '';
				$upload_datas[$column->Field] = $hinhanh;
			}
			else if(strpos($column->Type,'tinyint') === 0)
			{
				if($this->input->post($column->Field))	
					$data[$column->Field] = 1;
				else
					$data[$column->Field] = 0;
			}
			else
			{
				$data[$column->Field] = $this->input->post($column->Field);
			}
		}
		
		if(isset($thumbnailColumns))
		{
			foreach($thumbnailColumns as $key=>$value)
			{
				if($data[$value])
				{
					$config = array();
					$config['image_library'] = 'gd2';
					$config['source_image'] = $data[$value];
					$config['create_thumb'] = TRUE;
					$config['width'] = $thumbnailWidth;
					$config['height'] = $thumbnailHeight;
					$this->image_lib->initialize($config);
					if($this->image_lib->resize())
					{
						$data[$key] = $this->m_upload_path . $upload_datas[$value]['raw_name'] . '_thumb' . $upload_datas[$value]['file_ext'];
					}
				}
			}
		}
		
		$this->db->insert($table, $data);
	}
	function do_upload_file($filename='')
	{	$this->load->library('upload');
		$config['upload_path'] = './' . $this->m_upload_path;
		$config['allowed_types'] = 'doc|xls|pdf|docx|chm|txt|zip|rar|jpg|gif|jpeg|png|swf';
		$config['max_size']	= '220000';
		$this->upload->initialize($config);
		$this->load->library('upload', $config);
		if ( ! $this->upload->do_upload($filename))
		{
			$error = array('error' => $this->upload->display_errors());
			//echo $config['upload_path'];
			//print_r($error);
			//$this->load->view('upload_form', $error);
			//echo $error['error'];
			
			return '';
		}	
		else
		{
			$data = $this->upload->data();
			
//			$this->_baogia($userid);
			//print_r($data);
			//return $data['file_name'];
			return $data;
			//echo $data['file_name'];
		}
	}	
	
	function sua($table, $imageColumns=NULL, $thumbnailColumns=NULL, $thumbnailWidth=0, $thumbnailHeight=0)
	{
		$query = $this->db->query('show columns from ' . $table);
		$allColumns = $query->result();
		$keyColumns = array();
		$normalColumns = array();
		$prop = array();
		$upload_datas = array();
		foreach($allColumns as $column)
		{
			if($column->Key == 'PRI')
				$keyColumns[$column->Field] = $column->Field;
			else
				$normalColumns[$column->Field] = $column->Field;
			$prop[$column->Field] = array('Field'=>$column->Field, 'Type'=>$column->Type, 'Null'=>$column->Null, 'Key'=>$column->Key, 'Default'=>$column->Default, 'Extra'=>$column->Extra);
		}
		if(count($keyColumns) == 0)
			show_error("Bảng $table trong cơ sở dữ liệu không có khóa chính");
		foreach($keyColumns as $column)
		{
			if($prop[$column]['Type'] == 'datetime')
			{
				$this->db->where($column, date('Y-m-d H:i:s', get_form_date_value($column)));
			}
			else
			{
				$this->db->where($column, $this->input->post($column));
			}
		}
		
		$data = array();
		foreach($normalColumns as $column)
		{
			if($prop[$column]['Type'] == 'datetime' || $prop[$column]['Type'] == 'date')
			{
				$data[$column] = date('Y-m-d H:i:s', get_form_date_value($column));
			}
			else if($this->_isImageColumn($column, $imageColumns))
			{
				//$do_upload_file
				
//				echo $this->do_upload_file($column)->file_name;
				//print_r($this->do_upload_file($column));
				$hinhanh = $this->do_upload_file($column);
				
				$upload_data = $this->upload->data();
				if(!empty($hinhanh)){
					$data[$column] = $this->m_upload_path . $hinhanh['file_name'];
				}else{
					//$data[$column]='';
				}
				//$upload_datas[$column] = $hinhanh;
//				echo $hinhanh['file_name'];
				//exit();
				/*//neu la file anh
				$config['upload_path'] = './' . $this->m_upload_path;
				$config['allowed_types'] = 'gif|jpg|png|bmp|swf|zip|rar|doc|pdf';
				$config['max_size'] = '1048576';
				$config['max_width'] = '32768';
				$config['max_height'] = '32768';
				$this->upload->initialize($config);
				if(!$this->upload->do_upload($column))
				{
				}
				else
				{
					$upload_data = $this->upload->data();
					$data[$column] = $this->m_upload_path . $upload_data['file_name'];
					$upload_datas[$column] = $upload_data;
				}*/
			}
			else if(strpos($prop[$column]['Type'], 'tinyint') === 0)
			{
				$data[$column] = ($this->input->post($column) != NULL)?1:0;
			}
			else if(isset($thumbnailColumns) && isset($thumbnailColumns[$column]))
			{
			}
			else
			{
				$data[$column] = $this->input->post($column);
			}
		}
		
		if(isset($thumbnailColumns))
		{
			foreach($thumbnailColumns as $key=>$value)
			{
				if(isset($data[$value]) && $data[$value])
				{
					$config = array();
					$config['image_library'] = 'gd2';
					$config['source_image'] = $data[$value];
					$config['create_thumb'] = TRUE;
					$config['width'] = $thumbnailWidth;
					$config['height'] = $thumbnailHeight;
					$this->image_lib->initialize($config);
					if($this->image_lib->resize())
					{
						$data[$key] = $this->m_upload_path . $upload_datas[$value]['raw_name'] . '_thumb' . $upload_datas[$value]['file_ext'];
					}
				}
			}
		}
		$this->db->update($table, $data);
		
//		print_r($data);
//		print_r($prop);
	}
	function doimatkhau($tenadmin, $matkhau)
	{
		$this->db->where('taikhoan', $tenadmin);
		$params['matkhau'] = md5($matkhau);
		$this->db->update('tbluser', $params);
	}
	function taomoithanhvien($tenadmin, $matkhau, $quyen, $email, $dienthoai, $hoten, $diachi)
	{
		$params = array();
		$params['taikhoan'] = $tenadmin;
		$params['matkhau'] = md5($matkhau);
		$params['quyen'] = $quyen;
		$params['email'] = $email;
		$params['dienthoai'] = $dienthoai;
		$params['hoten'] = $hoten;
		$params['diachi'] = $diachi;
		$this->db->insert('tbluser', $params);
	}
	function suataikhoan()
	{
		if($this->input->post('id') === FALSE)
			show_error('Thông tin không chính xác');
		$this->db->where('ID_admin', $this->input->post('id'));
		$params = array();
		$params['active'] = $this->input->post('active')?1:0;
		$params['quyen'] = $this->input->post('quyen');
		
		$this->db->update('tbladmin', $params);
	}
	function checkchange($table, $id, $column, $oldValue)
	{
		$query = $this->db->query("show columns from $table");
		foreach($query->result() as $row)
		{
			if($row->Key == 'PRI')
			{
				$keyField = $row->Field;
				break;
			}
		}
		if(!isset($keyField))
			show_error("bảng không có khóa chính");
		$this->db->where($keyField, $id);
		$params[$column] = !$oldValue;
		$this->db->update($table, $params);
	}
}
?>